// pages/teacher_current_reports/teacher_current_reports.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    boxId:"",
    reports:[],
    box:{},
    scoreItems:[],
    score_item:"",
    isShowKeyboard:false,
    keys:[1,2,3,4,'后退',5,6,7,8,9,0,'A','B','C','D','E','F','+','-','确认'],
    selecteReportId:"",
    is_ai_score_open:false,
    loading:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options != null){
      const boxId= options.boxId;
      const score_item= options.score_item;
      this.setData({boxId,score_item});
      this.getReportBox(boxId)
      wx.request({
        url: app.globalData.url+'/wechat/box_reports_all?boxId='+boxId+"&score_item="+score_item,
        method: 'get',
        success: res2 => {
          const reports=res2.data;
          this.setData({reports});
          wx.setNavigationBarTitle({
            title: score_item
        })
        }
      })
    }
  },
  getReportBox:function(boxId){ 
    wx.request({ 
      url: app.globalData.url+'/wechat/box?boxId='+boxId, 
      method: 'get', 
      success: res2 => { 
        const box=res2.data; 
        const is_ai_score_open=box&&box.is_ai_score_open||false; 
        this.setData({is_ai_score_open}); 
        
      } 
    }) 
  }, 
  view_report:function(e){
      const username =e.target.id;
        wx.navigateTo({
            url: "../report_comment/report_comment?username="+username+"&boxId="+this.data.boxId+"&score_item="+this.data.score_item
          });
  },
  showKeyboard:function(e){
    const reportId=e.target.id;
    this.setData({selecteReportId:reportId,isShowKeyboard:true})
  },
  hideKeyboard:function(){

    wx.request({
      url: app.globalData.url+'/wechat/box_reports_all?boxId='+this.data.boxId+"&score_item="+this.data.score_item,
      method: 'get',
      success: res2 => {
        const reports=res2.data;
        this.setData({reports,isShowKeyboard:false});
      }
    })

  },

  clickBoard:function(e){
    const key=e.target.id;
    const reports=this.data.reports
    const report=reports.find(r=>r.objectId===this.data.selecteReportId);
    const index=reports.findIndex(r=>r.objectId===this.data.selecteReportId);
    const text = report.r_score||"";
    const reg = /[a-zA-Z0-9]/;
    let newText = text;
    if (reg.test(key)||key=='+'||key=="-") {
        newText = text + (key + '');
        report.r_score=newText;
        reports[index]=report
        this.setData({reports})
    } else {
        switch (key) {
            case "后退": {
                newText = text.substring(0, text.length - 1);
                report.r_score=newText;
                reports[index]=report
                this.setData({reports})
                break;
            }
            case "确认": {
                this.onOK(report);
                break;
            }

        }
    }
  
  },

  onOK:function(report){
    wx.request({
      url: app.globalData.url+'/wechat/mark_r_score',
      data: {
        report,
        score_item:this.data.score_item,
        boxId:this.data.boxId
  },
  method: 'POST',
   header: {
    'content-type': 'application/json'
   },
  success: res2 => {
    this.setData({isShowKeyboard:false})
}
})
    console.log('report',report)
  },

  getAiResult:function(){
    this.setData({loading:true})
    wx.request({
      url: app.globalData.url+'/wechat/box_image_ai_result',
      method: 'post',
      data:{
        boxId:this.data.boxId
      },
      success: res2 => {
       console.log("box_image_ai_result")
      }
    })
    this.setData({loading:false})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
   
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
  //   wx.navigateTo({
  //     url: "../teacher_m_report_box/teacher_m_report_box?boxId="+this.data.boxId,
  // });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})